﻿using System;
using System.Linq;
using Asistente.Managers;
using Asistente.Model.General;
using System.Collections.ObjectModel;

namespace Asistente.Model.Representation
{
    //Surge por la necesidad de representar entidades compuestas. (Para identificar los RET paras las funciones de datos)
    //Un master detail tiene un panel editbable maestro, y un conjunto de panels editables que corresponden con los detalles.

    public class MasterDetail : Element,IElement
    {
        public String Name { get; set; }
        public EditablePanel Master { get; set; }
        public Collection<EditablePanel> Details { get; set; }
    
        public MasterDetail()
        {
            Details = new Collection<EditablePanel>();
        }

        public MasterDetail Clone()
        {
            MasterDetail copy = (MasterDetail) this.MemberwiseClone();
            Collection<EditablePanel> panels = new Collection<EditablePanel>();

            copy.Master = (EditablePanel)Master.Clone();

            foreach (var panel in Details)
            {
                panels.Add((EditablePanel)panel.Clone());
            }
            copy.Details = panels;

            return copy;
        }

        private Panel CreatePanel(Panel panel)
        {
            panel.Name = Numerator.Instance.GetName(panel.GetType());            
            return panel;
        }

        public Form NewForm()
        {
            return CreatePanel(new Form()) as Form;
        }

        public Filter NewFilter()
        {
            return CreatePanel(new Filter()) as Filter;
        }

        public Grid NewGrid()
        {
            return CreatePanel(new Grid()) as Grid;
        }

        internal EditablePanel NewMasterFilter()
        {
            EditablePanel editablePabel = NewFilter();
            Master = editablePabel;
            return editablePabel;
        }

        internal EditablePanel NewMasterForm()
        {
            EditablePanel editablePabel = NewForm();
            Master = editablePabel;
            return editablePabel;
        }

        internal EditablePanel NewMasterGrid()
        {
            EditablePanel editablePabel = NewGrid();
            Master = editablePabel;
            return editablePabel;
        }

        internal EditablePanel NewDetailFilter()
        {
            EditablePanel editablePabel = NewFilter();
            Details.Add(editablePabel);
            return editablePabel;
        }

        internal EditablePanel NewDetailForm()
        {
            EditablePanel editablePabel = NewForm();
            Details.Add(editablePabel);
            return editablePabel;
        }

        internal EditablePanel NewMDetailGrid()
        {
            EditablePanel editablePabel = NewGrid();
            Details.Add(editablePabel);
            return editablePabel;
        }

       
        public void DeletePanel(Panel panel)
        {
            if (Master == panel)
            {
                Master = null;
            }
            if (Details.Contains(panel))
            {
                Details.Remove((EditablePanel)panel);
            }
        }
    }
}
